<!--
  功能：确认方向
  作者：licy
  邮箱：licy@briup.com
  时间：2020年12月01日 22:17:39
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='confirm_direction'>
    <!-- 按钮 -->
    <div class="btns">
      <div style="text-align:center;padding-buttom:.5em">
        <el-form size="small" :inline='true'>
        <el-form-item>
          <el-cascader filterable v-model="param.channelId" clearable :options="channels" placeholder="请选择渠道"
          :props="{ children:'children',label:'name',value:'id', emitPath: false}" ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-cascader filterable v-model="param.clazzId" clearable :options="directionClazzTree" placeholder="请选择班级"
        :props="{ children:'clazzes',label:'name',value:'id',emitPath: false}" ></el-cascader>
        </el-form-item>
        <el-form-item >
          <el-select v-model="param.status" placeholder="状态" style="width:120px" clearable>
            <el-option value="报名中" label="报名中"></el-option>
            <el-option value="待选方向" label="待选方向"></el-option>
            <el-option value="待分班" label="待分班"></el-option>
            <el-option value="学习中" label="学习中"></el-option>
          </el-select>  
        </el-form-item>
        <el-form-item >
          <el-input placeholder="姓名" v-model="param.realname" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="loadApplys">搜索</el-button>
        </el-form-item>
      </el-form>
      </div>
    </div>
    <div style="text-align:right">
      <el-button type="primary" size="small" @click="toBatchConfirmDirection">批量选方向</el-button>
    </div>
    <!-- 按钮 -->
    <!-- 表格 -->
    <el-table :data="applyData.list" size="small" v-loading="loading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" > </el-table-column>
      <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
      <el-table-column prop="realname" label="姓名" width="90" align="center" fixed="left"/>
      <el-table-column prop="idCard" label="身份证号" width="180" align="center"/>
      <el-table-column prop="status" label="状态" width="80" align="center" fixed="left">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status ==='报名中'" type="danger" size="mini">{{scope.row.status}}</el-tag>
            <el-tag v-else-if="scope.row.status==='待分班'" type="warning" size="mini">{{scope.row.status}}</el-tag>
            <el-tag v-else type="success" size="mini">{{scope.row.status}}</el-tag>
          </template>
        </el-table-column>
      <el-table-column prop="direction" label="学习方向" width="180" align="center">
        <template slot-scope="scope">
          <el-tag size="mini" v-if="!scope.row.direction" type="danger">未选定</el-tag>
          <el-tag size="mini" v-else>{{scope.row.direction}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="确认时间" width="150" align="center">
        <template slot-scope="scope">
          {{scope.row.directionTime | fmtTime}}
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="60" align="center"/>
      <el-table-column prop="channelId" label="渠道编号" width="80" align="center"/>
      <!-- <el-table-column prop="school" label="学校" width="120" align="center"/>
      <el-table-column prop="college" label="学院" width="120" align="center"/>
      <el-table-column prop="profession" label="专业" width="120" align="center"/>
      <el-table-column prop="schoolClazz" label="学校班级" width="120" align="center"/> -->
      <el-table-column prop="isAttention" label="是否关注" min-width="120" align="center"/>
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.status !== '待选方向'" type="text" size="mini" @click.prevent="toConfirmDirection(scope.row)">选方向</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格 -->
    <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="applyData.total" 
          :page-size="applyData.pageSize" 
          :current-page="applyData.page"
          :page-sizes="[ 10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="40%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <div class="sub_title">学生信息</div>
        <el-table size="small" :data="students">
            <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
          <el-table-column prop="realname" label="姓名" width="90" align="center" fixed="left"/>
          <el-table-column prop="idCard" label="身份证号" align="center"/>
        </el-table>
        <div class="sub_title">方向确认</div>
        <el-form ref="confirm_form" :model="form" :rules="rules" size="mini" label-position="center">
          <el-form-item prop="directionId">
            <el-radio-group v-model="form.directionId">
              <div v-for="d in directionData.list" :key="d.id" style="line-height:30px">
                <el-radio :label="d.id" >{{d.name}}</el-radio>
              </div>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="submitHandler">确 定</el-button>
      </span>
    </Briupdrawer>
    <!-- /抽屉 -->
  </div>
</template>

<script>
import {get,post,postJSON} from '@/utils/request'
import _ from 'lodash'
import qs from 'qs';
import { mapGetters } from 'vuex'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
      form: {},
      loading:false,
      visible: false,
      visible_import:false,
      title: '确认方向',
      param:{
        page:1,
        pageSize:15
      },
      students:[],
      import_data:{},
      applyData:{list:[]},
      directionData:{list:[]},
      rules: {
        directionId: [ { required: true, message: '请选择方向', trigger: 'change' } ]
      }
   }
  },
  // 计算属性
  computed: {
    ...mapGetters(['channels','directionClazzTree'])
  },
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 提交处理函数
    submitHandler() {
      this.$refs['confirm_form'].validate((valid) => {
        if (valid) {
          let url = '/student/batchConfirmDirection'
          let ids = this.students.map(item => item.id);
          this.form.ids = ids;
          postJSON(url,this.form).then(response => {
            this.$message({ message: response.message, type: 'success' })
            this.loadApplys()
            this.visible = false
          });
        } else {
          return false
        }
      })
    },
    handleSelectionChange(val) {
      this.students = val;
    },
    // 去批量确认方向
    toBatchConfirmDirection(){
      if(this.students == 0){
        this.$message({ message: '请先选择学生', type: 'error' })
        return false;
      }
      this.visible = true;
    },
    // 去确认方向
    toConfirmDirection(row){
      this.students = [row]
      this.visible = true;
    },
    // 加载报名信息
    loadApplys(){
      this.loading = true;
      let url = '/student/pageQuery'
      // 去除空值
      for(let key in this.param){
        if(!this.param[key]){
          delete this.param[key]
        }
      }
      delete this.param.channelIds;
      if(this.param.channelId){
        this.param.channelIds = [this.param.channelId]
      }
      get(url+"?"+qs.stringify(this.param,{ arrayFormat: 'repeat' }))
      .then(response => {
        this.applyData = response.data
        this.loading = false;
      })
    },
    
    // 加载方向
    loadDirection(){
      let url = "/direction/pageQuery"
      get(url,{page:1,pageSize:100}).then(response => {
        this.directionData = response.data;
      })
    },
    //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val;
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadApplys();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadApplys();
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载报名信息
    this.loadApplys();
    // 加载方向信息
    this.loadDirection();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>

</style>
